//
//  PopoverPresentationController.swift
//  weibo
//
//  Created by sanya on 15/6/27.
//  Copyright © 2015年 sanya. All rights reserved.
//

import UIKit
class PopoverPresentationController: UIPresentationController {
    
    /// 展现视图的frame
    var presentedFrame  = CGRectZero
    /**
    containerView 容器视图，放置展现出来的视图的
    presentedView() 被展现出来的视图的
    
    */
    ///  实例化转场控制器
    ///
    ///  :param: presentedViewController  被`展现`的控制器
    ///  :param: presentingViewController 文档上说，发起的控制器，Xcode 6 nil，Xcode 7 野指针
    ///
    ///  :returns: 展现控制器
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        // 添加点按手势
        prepareDummyView()
        
    }
    /// 添加手势
    private func prepareDummyView(){
        let tap = UITapGestureRecognizer(target: self, action: "close")
        dummyView.addGestureRecognizer(tap)
    }
    /// 关闭控制器
    func close(){
       NSNotificationCenter.defaultCenter().postNotificationName("changeBtnstatus", object: nil)
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    ///  容器视图将要重新布局子视图
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        // 添加并且设置遮罩视图
        dummyView.frame = containerView!.bounds
        containerView?.insertSubview(dummyView, atIndex: 0)
        // 设置视图大小
        presentedView()?.frame = presentedFrame
    }
    // 遮罩视图——懒加载
    lazy var dummyView:UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(white: 0, alpha: 0.2)
        return v
    }()
}
